import {reqGetCategory1ListData,reqGetCategory2ListData, reqGetCategory3ListData} from '@/api/product/category'
import {defineStore} from 'pinia'
import type { CategoryStoreModel } from '@/api/product/model/categoryStoreModel'

export const useCategoryListStore = defineStore('categoryList',{

  state:():CategoryStoreModel=>({
    category1Id:undefined,
    category2Id:undefined,
    category3Id:undefined,

    category1List:[],
    category2List:[],
    category3List:[],
  }),

  actions:{
    // 获取一级分类数据
    async getCategory1ListData(){
      const result = await reqGetCategory1ListData()
      this.category1List = result
    },
    // 获取二级分类数据
    async getCategory2ListData(category1Id:number){
      this.category1Id = category1Id
      const result = await reqGetCategory2ListData(category1Id)
      this.category2List = result 
      // 清空二三级数据
      this.category2Id = undefined
      this.category3Id = undefined
      this.category3List = []
    },
    // 获取三级分类数据
    async getCategory3ListData(category2Id: number){
      this.category2Id = category2Id
      const result = await reqGetCategory3ListData(category2Id)
      this.category3List = result
      // 清空category3Id
      this.category3Id = undefined
    },
    setCategory3Id(category3Id: number){
      this.category3Id = category3Id
    }
    },
    getters:{}
})